﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace WidgetLibrary
{
    /// <summary>
    /// Communication Class beetween widget and native code.
    /// Listen to Widget and manage it.
    /// The Widget must launch a script in this format.
    ///     window.external.Notify("function(parameters)");
    /// </summary>
    public static class ManageWidget
    {
        private static string[] _separator = { "(", ")"};
        private static string[] _separator2 = { ","};

        public static int Manage(string action, WebBrowser webBrowser)
        {
            int res = 0;
            Widget widget = new Widget(webBrowser);

            string[] temp = action.Split(_separator, StringSplitOptions.None);
            string function = temp[0];
            string[] temp2 = temp[1].Split(_separator2, StringSplitOptions.None);
            switch (function)
            {
                case WidgetFunctions.PlayMusicBackground:
                    widget.PlayMusicBackground(temp[1]);
                    res = 1;
                    break;
                case WidgetFunctions.ShowNotification:
                    widget.ShowNotification(temp[1]);
                    res = 1;
                    break;
                case WidgetFunctions.GetLanguage:
                    //Widget.GetLanguage();
                    break;
                case WidgetFunctions.OpenURL:
                    widget.OpenURL(temp2[0]);
                    res = 1;
                    break;
                case WidgetFunctions.Back:
                    widget.Back();
                    res = 1;
                    break;
                case WidgetFunctions.PlayAudio: //.wav files
                    widget.PlayAudio(temp2[0], temp2[1]);
                    res = 1;
                    break;
                case WidgetFunctions.StopAudio:
                    widget.StopAudio();
                    res = 1;
                    break;
                case WidgetFunctions.PlayVideo: 
                    widget.PlayVideo(temp[1]);
                    res = 1;
                    break;                
                case WidgetFunctions.DownloadFile:
                    widget.DownloadFile(temp[1]);
                    res = 1;
                    break;
                case WidgetFunctions.SaveConfiguration: //Save a config item from widget in Isolated Storage
                    widget.SaveConfiguration(temp[1]);
                    res = 1;
                    break;
                case WidgetFunctions.LoadConfiguration: //Load the config item in Widget.
                    widget.LoadConfiguration();
                    res = 1;
                    break;
                case WidgetFunctions.HistoryBackEnd:
                    res = 2;
                    break;
                default:
                    break;
            }
            return res;
        }        

    }
}
